package cn.openea.perpartner.space.web.controller;


import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.space.web.entity.AcceptLog;
import cn.openea.perpartner.space.web.service.IAcceptLogService;
import cn.openea.perpartner.space.web.service.IShareLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (AccessLog)表控制层
 *
 * @author Weird077
 * @since 2023-03-22 15:19:09
 */
@Api(tags = "海上新空间")
@RestController
@RequestMapping("/space/accept")
public class AcceptController extends BaseController {
    @Resource
    private IAcceptLogService acceptLogService;
    @Resource
    private IShareLogService shareLogService;

    @ApiOperation(value = "邀请列表")
    @PostMapping("/list")
    public TableDataInfo selectAcceptLogList(@RequestBody AcceptLog acceptLog) {
        startPage();
        List<AcceptLog> list = this.acceptLogService.selectAcceptLogList(acceptLog);
        return getDataTable(list);
    }

    @ApiOperation(value = "邀请码验证")
    @PostMapping("/verify")
    public AjaxResult selectOne(@RequestBody AcceptLog acceptLog) {
        String shareId = acceptLog.getShareId();
        if (acceptLogService.checkShareIdUnique(shareId)) {
            return error("邀请码(" + shareId + ")已被使用！");
        }
        if (shareLogService.checkShareIdUnique(shareId)) {
            return error("邀请码(" + shareId + ")不存在！");
        }
        boolean isSuccess = acceptLogService.saveAccessLog(acceptLog);
        return isSuccess ? success() : error("邀请码(" + shareId + ")验证失败！");
    }

    @ApiOperation(value = "验证是否会员")
    @PostMapping("/checkUser")
    public AjaxResult checkUser(@RequestBody AcceptLog acceptLog) {
        String userId = acceptLog.getUserId();
        if (StrUtil.isBlank(userId)) {
            return error("用户id不能为空！");
        }
        if (acceptLogService.checkIsVip(userId)) {
            return error("当前用户非会员！");
        }
        return success("验证通过！");
    }

}

